/**
 * 
 */
package nth.randcats.client;

import static com.google.gwt.query.client.GQuery.$;
import static gwtquery.plugins.ui.Ui.Ui;
import gwtquery.plugins.ui.widgets.Progressbar;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.query.client.GQuery;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

/**
 * @author Nick Heiner <nth23@cornell.edu>
 */
public class RandCatsView extends Composite {

	public static final String EMAIL_REGEX = "[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?";

	protected static final String INVALID_TEXT_STYLE = "invalid";
	final String INVALID_EMAIL_MSG = "Enter the email you use to log in to Facebook";

	private static RandCatsViewUiBinder uiBinder = GWT
			.create(RandCatsViewUiBinder.class);

	interface RandCatsViewUiBinder extends UiBinder<Widget, RandCatsView> {
	}

	/**
	 * Because this class has a default constructor, it can be used as a binder
	 * template. In other words, it can be used in other *.ui.xml files as
	 * follows: <ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder"
	 * xmlns:g="urn:import:**user's package**">
	 * <g:**UserClassName**>Hello!</g:**UserClassName> </ui:UiBinder> Note that
	 * depending on the widget that is used, it may be necessary to implement
	 * HasHTML instead of HasText.
	 */
	public RandCatsView() {
		initWidget(uiBinder.createAndBindUi(this));

		progressBar = $("#progressbar").hide();
		progressBar.as(Ui).progressbar(Progressbar.Options.create().value(100));
		
		KeyPressHandler textKeyPress = new KeyPressHandler() {

			@Override
			public void onKeyPress(KeyPressEvent event) {
				final boolean isValid = emailText.getText().matches(EMAIL_REGEX);
				sendButton.setEnabled(isValid && !passwordText.getText().isEmpty());
				
				if (event.getCharCode() == KeyCodes.KEY_ENTER) {
					sendButton.click();
				}
			}
		};

		emailText.addKeyPressHandler(textKeyPress);
		passwordText.addKeyPressHandler(textKeyPress);

		emailText.setFocus(true);
		emailText.selectAll();
	}
	
	public void showProgressBar(String loadingMsg) {
		progressMsg.setText(loadingMsg);
		progressMsg.setVisible(true);
		progressBar.show();
	}
	
	public void hideProgressBar() {
		progressBar.hide();
		progressMsg.setVisible(false);
	}

	@UiHandler("emailText")
	public void onemailChange(ChangeEvent event) {
		final boolean isValid = emailText.getText().matches(EMAIL_REGEX);
		emailError.setText(isValid ? "" : INVALID_EMAIL_MSG);

		if (isValid) {
			emailText.removeStyleName(INVALID_TEXT_STYLE);
		} else {
			emailText.addStyleName(INVALID_TEXT_STYLE);
		}
	}

	@UiHandler("sendButton")
	void submit(ClickEvent event) {
		if (!emailText.getText().matches(EMAIL_REGEX) || passwordText.getText().isEmpty()) {
			return;
		}

		String historyToken = emailText.getText() + RandCats.ARG_DELIMITER
				+ passwordText.getText();
		History.newItem(historyToken);
	}

	@UiField
	Label instructions;

	@UiField
	Image image;

	@UiField
	TextBox emailText;

	@UiField
	Label emailError;
	
	@UiField
	Label progressMsg;

	@UiField
	PasswordTextBox passwordText;

	@UiField
	Label passwordError;

	@UiField
	Button sendButton;

	private GQuery progressBar;

	public void setInstruction(String text) {
		instructions.setText(text);
	}

	public void hideImage() {
		image.setVisible(false);
	}

	public void showImage(String url) {
		image.setUrl(url);
		image.setVisible(true);
	}

}
